import request from "@/utils/request";

/**
 * 基础增删改查API
 * @param baseUrl
 */
const useBaseApi = (baseUrl: string) => {
    /**
     * 按ID获取一行数据
     * @param id
     * @returns
     */
    function callGetOne(id: number) {
        return request({
            url: baseUrl + "/" + id,
            method: "get",
        });
    }

    /**
     * 分页获取参数
     * @param query 参数列表
     */
    function callPage(query?: any) {
        return request({
            url: baseUrl + "/page",
            method: "post",
            params: query,
        });
    }
    /**
     * 分页获取参数
     * @param query 参数列表
     */
    function callPageList(query?: any) {
        return request({
            url: baseUrl + "/page",
            method: "post",
            data: query,
        });
    }

    /**
     * 获取列表
     * @param query 参数列表
     */
    function callList(query?: any) {
        return request({
            url: baseUrl + "/list",
            method: "get",
            params: query,
        });
    }

    /**
     * in方式获取数据
     * @param ids ids[]
     */
    function callIn(ids: Array<number>) {
        return request({
            url: baseUrl + "/in/" + ids.join(","),
            method: "get",
        });
    }

    /**
     * 保存
     * @param data 需要保存的数据
     */
    function callSave(data?: any) {
        return request({
            url: baseUrl,
            method: "post",
            data,
        });
    }
    /**
     * 批量保存
     * @param data 需要保存的数据
     */
    function callBatchSave(data?: any) {
        return request({
            url: baseUrl + "/saves",
            method: "post",
            data,
        });
    }

    /**
     * 修改
     * @param data 需要修改的数据
     */
    function callModify(data?: any) {
        return request({
            url: baseUrl,
            method: "put",
            data,
        });
    }
    /**
     * 批量修改
     * @param data 需要修改的数据
     */
    function callBatchModify(data?: any) {
        return request({
            url: baseUrl + "/modifys",
            method: "put",
            data,
        });
    }

    /**
     * 批量删除
     * @param ids 数据ids
     */
    function callRemoves(ids: Array<number>) {
        return request({
            url: baseUrl + "/" + ids,
            method: "delete",
        });
    }

    function callSubGet(query?: any) {
        return request({
            url: baseUrl,
            method: "get",
            params: query,
        });
    }
    /**
     * 分页获取参数(在地址后面跟分页信息)
     * @param query 参数列表 query.current页码 query.size每页条目数
     */
    function callSplicPage(query?: any) {
        return request({
            url: baseUrl + "/" + query.current + "/" + query.size,
            method: "post",
            params: query,
        });
    }

    return {
        callGetOne,
        callPage,
        callPageList,
        callList,
        callSave,
        callBatchSave,
        callModify,
        callBatchModify,
        callRemoves,
        callSplicPage,
        callSubGet,
        callIn,
    };
};

// get 请求
export const callGet = (url: string, query?: any) => {
    return request({
        url,
        method: "get",
        params: query,
    });
};

export default useBaseApi;
